package com.abe.libquick.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;

import com.abe.libcore.utils.CommonUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class CopyFile {

    public Context context;
    public CopyCallbackListener listener;
    private String sourceUrl;
    private String sourceFileName;
    private String targetUrl;
    private String targetFileName;
    private boolean isAssets;

    public CopyFile(String sourceUrl, String sourceFileName, String targetUrl,
                    String targetFileName, boolean isAssets, Context context, CopyCallbackListener listener) {
        this.sourceUrl = sourceUrl;
        this.sourceFileName = sourceFileName;
        this.targetUrl = targetUrl;
        this.targetFileName = targetFileName;
        this.isAssets = isAssets;
        this.context = context;
        this.listener = listener;
        CopyAsy mCopyAsy = new CopyAsy();
        mCopyAsy.execute("");
    }

    private boolean init() {
        try {
            File mFile = new File(targetUrl);
            if (!mFile.exists()) {
                boolean success = mFile.mkdirs();
                CommonUtils.INSTANCE.log("success:" + success);
            }
            File targetFile = new File(targetUrl + "/" + targetFileName);
            if (!targetFile.isFile()) {
                boolean success = targetFile.createNewFile();
                CommonUtils.INSTANCE.log("success:" + success);
            }
            if (isAssets) {
                InputStream is = context.getAssets().open(sourceFileName);
                FileOutputStream fos = new FileOutputStream(targetFile);
                byte[] temp = new byte[1024];
                int iTTF;
                while ((iTTF = is.read(temp)) > 0) {
                    fos.write(temp, 0, iTTF);
                }
                fos.close();
                is.close();
            } else {
                File sourceFile = new File(sourceUrl + "/" + sourceFileName);
                InputStream is = new FileInputStream(sourceFile);
                FileOutputStream fos = new FileOutputStream(targetFile);
                byte[] temp = new byte[1024];
                int iTTF;
                while ((iTTF = is.read(temp)) > 0) {
                    fos.write(temp, 0, iTTF);
                }
                fos.close();
                is.close();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /*
     * 回调函数接口
     */
    public interface CopyCallbackListener {
        void callBack(boolean copy);
    }

    @SuppressLint("StaticFieldLeak")
    private class CopyAsy extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            if (init()) {
                return "1";
            } else {
                return "0";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (listener != null) {
                CopyCallbackListener mCallbackListener = listener;
                mCallbackListener.callBack(result.equals("1"));
            }
        }
    }
}